
public class LexicalAnalyzerDFA extends LexicalAnalyzer {
    public LexicalAnalyzerDFA(String text){
        super(text);
    }

    public int handleFirstAlpha(int arg, String arg0){
        int i=arg;
        String s = arg0;
        char ch=text.charAt(++i);
        while(isAlpha(ch)==1 || isNumber(ch)==1){
            s = s+ch;
            ch=text.charAt(++i);
        }
        if(isKey(s)==1){
            // 输出key
            printResult(s, "关键字");
            return i;

        }
        else {
            // 输出普通的标识符
            printResult(s, "标识符");
            return i;
        }
    }

    public int handleFirstNum(int arg, String arg0) {
        int i = arg;
        char ch = text.charAt(++i);
        String s = arg0;
        // 整数部分
        while(isNumber(ch)==1){
            s = s+ch;
            ch = text.charAt(++i);
        }
        if((text.charAt(i)==' ')||
                (text.charAt(i)=='\t')||
                (text.charAt(i)=='\n')||
                (text.charAt(i)=='\r')||
                (text.charAt(i)=='\0')||ch==';'||ch==','||ch==')'||ch==']'||ch=='['||ch=='('||ch==':'){
            // 到了结尾，输出数字
            printResult(s, "整数");
            return i;
        }
        else if (ch=='E' || ch == 'e') {
            return handleSciNot(i, s);
        }
        else if (text.charAt(i)=='.') {
            return  handleFloat(i, s);
        }
        // 浮点数判断 小数部分
        /*
        else if (text.charAt(i)=='.'&&(isNumber(text.charAt(i+1))==1)) {
            s = s +'.';
            ch = text.charAt(++i);
            while (isNumber(ch)==1) {
                s = s+ch;
                ch = text.charAt(++i);
            }
            if (ch=='E' || ch=='e') {
                if (text.charAt(i+1)=='+' || text.charAt(i+1)=='-') {
                    // 存入E
                    s = s+ch;
                    ch = text.charAt(++i);
                    s = s+ch;
                    ch = text.charAt(++i);
                    while (isNumber(ch)==1) {
                        s = s+ch;
                        ch = text.charAt(++i);
                    }
                    if(ch=='\r'||ch=='\n'||ch==';'||ch=='\t'){
                        printResult(s, "科学计数");
                        return i;
                    }
                    else {
                        printError(i, s, "浮点数错误");
                        return i;
                    }
                }
                else if (isNumber(text.charAt(i+1))==1) {
                    s = s+ch;
                    ch = text.charAt(++i);
                    while (isNumber(ch)==1) {
                        s = s+ch;
                        ch = text.charAt(++i);
                    }
                    if(ch=='\r'||ch=='\n'||ch==';'||ch=='\t'){
                        printResult(s, "科学计数");
                        return i;
                    }
                    else {
                        printError(row_number, s, "浮点数错误");
                        return i;
                    }
                }
                else {
                    printError(row_number, s, "科学计数法错误");
                    return ++i;
                }
            }
            else if (ch=='\n'||ch=='\r'||ch=='\t'||ch==' '||ch=='\0'||ch!=','||ch!=';') {
                printResult(s, "浮点数");
                return i;
            }
            else if (ch=='+'||ch=='-'||ch=='*'||ch=='/'||ch=='\0') {
                printResult(s, "浮点数");
                return i;
            }
            else {
                while (ch!='\n'&&ch!='\t'&&ch!=' '&&ch!='\r'&&ch!='\0'&&ch!=';'&&ch!='.'&&ch!=',') {
                    s = s+ch;
                    ch = text.charAt(++i);
                }
                printError(row_number, s, "不合法的字符");
                return i;
            } 
        }*/
        else if (ch=='+'||ch=='-'||ch=='*'||ch=='/'||ch=='\0') {
            printResult(s, "整数");
            return i;
        }
        else {
            do {
                ch = text.charAt(i++);
                s = s+ch;
            } while ((text.charAt(i)!=' ')
                    &&(text.charAt(i)!='\t')
                    &&(text.charAt(i)!='\n')
                    &&(text.charAt(i)!='\r')
                    &&(text.charAt(i)!='\0'));
            printError(row_number, s, "错误的标识符");
            return i;
        }
    }
    public int handleSciNot(int arg, String arg0){
        int i=arg;
        char ch=text.charAt(i);
        String s = arg0;
        if (text.charAt(i+1)=='+' || text.charAt(i+1)=='-') {
            s = s+ch;
            ch = text.charAt(++i);
            s = s+ch;
            ch = text.charAt(++i);
            
        }
        else if (isNumber(text.charAt(i+1))==1) {
            s = s+ch;
            ch = text.charAt(++i);
            
        }
        else {
            printError(row_number, s, "科学计数法错误");
            return ++i;
        }
        while (isNumber(ch)==1) {
            s = s+ch;
            ch = text.charAt(++i);
        }
        if(ch=='\r'||ch=='\n'||ch==';'||ch=='\t'){
            printResult(s, "科学计数");
            //double st = 2.e+0;
            return i;
        }
        else {
            printError(row_number, s, "浮点数错误");
            return i;
        }
    }
    public int handleFloat(int arg, String arg0){
        int i=arg;
        char ch=text.charAt(i);
        String s = arg0;
        s = s +'.';
        ch = text.charAt(++i);
        while (isNumber(ch)==1) {
            s = s+ch;
            ch = text.charAt(++i);
        }
        if (ch=='E' || ch=='e') {
            return handleSciNot(i, s);        
        }
        else if (ch=='\n'||ch=='\r'||ch=='\t'||ch==' '||ch=='\0'||ch!=','||ch!=';') {
            printResult(s, "浮点数");
            return i;
        }
        else if (ch=='+'||ch=='-'||ch=='*'||ch=='/'||ch=='\0') {
            printResult(s, "浮点数");
            return i;
        }
        else {
            while (ch!='\n'&&ch!='\t'&&ch!=' '&&ch!='\r'&&ch!='\0'&&ch!=';'&&ch!='.'&&ch!=',') {
                s = s+ch;
                ch = text.charAt(++i);
            }
            printError(row_number, s, "不合法的字符");
            return i;
        }
    }
    
}

    

